import requests
import json
import re
import linecache
import time
import hashlib


class CO1A(object):
    def config_list(sn, version, time, md5):  # post请求
        url = "http://open.dairphone.cn/device/config_list"
        headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        data = {
            'check_code': md5,
            'time': time,
            'sn': sn,
            'version': version
        }
        response = requests.request("post", url=url, headers=headers, data=data)
        r = response.json()
        output = json.dumps(r, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        f = json.dumps(r)
        file = open('E:\\json.txt', 'w', encoding='utf-8')
        file.write(f)
        print(output)

    def activation(sn, url1, version, time, md5, sign):  # post请求
        # url = 'https://f.triumen.cn/device/check_status'
        headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        data = {
            sign: md5,  # 生成md5
            'time': time,
            'sn': sn,
            'version': version
        }
        response = requests.request("post", url=url1, headers=headers, data=data)
        r = response.json()
        output = json.dumps(r, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        f = json.dumps(r)
        file = open('E:\\json.txt', 'w', encoding='utf-8')
        file.write(f)
        print(output)

    def back_sweep(sn, url3, token, version):  # post请求
        # url = 'https://f.triumen.cn/device/devfs'
        headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        data = {
            'auth_code': 'ciMX3qJ94e/UmKuVZ1RiKNQz',  # 需要提供auth_code，已写死
            'token': token,
            'sn': sn,
            'price': '1',
            'pay_type': '21',  # 目前仅支付宝刷脸设备需要提供
            'version': version
        }
        response = requests.request("post", url=url3, headers=headers, data=data)
        r = response.json()
        output = json.dumps(r, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        print(output)

    def card(sn, url6, token, version, time):  # get请求
        # url = 'https://b.triumen.cn/pay/list?plat=euLk6Dfe'
        headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        params = {
            'time': time,  # 生成时间戳
            'token': token,
            'sn': sn,
            'version': version
        }
        response = requests.request("get", url=url6, headers=headers, params=params)
        print(response)

    def check(sn, url7, token, version):  # post请求
        # url = 'https://f.triumen.cn/device/auth'
        headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        data = {
            'token': token,
            'sn': sn,
            'version': version
        }
        response = requests.request("post", url=url7, headers=headers, data=data)
        r = response.json()
        output = json.dumps(r, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        print(output)

    def log(sn, url12, uid, version):  # post请求
        # url = 'http://open.dairphone.cn/device/upload_log'
        headers = {
            'Content-Type': 'multipart/form-data;boundary=d5dde523-3723-439c-bcd7-79bc8e533ab2'
        }
        data = {
            'logfile': '2019-12-26_c01.txt',
            'sn': sn,
            'uid': uid,
            'version': version
        }
        response = requests.request("post", url=url12, headers=headers, data=data)
        r = response.json()
        output = json.dumps(r, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        print(output)

    def order(sn, url4, token, version):  # post请求
        # url = 'https://f.triumen.cn/device/queryorder'
        headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        data = {
            'order_no': '19122761500100003208',  # 可提供一个订单号复制进来替换
            'sn': sn,
            'token': token,
            'version': version
        }
        response = requests.request("post", url=url4, headers=headers, data=data)
        r = response.json()
        output = json.dumps(r, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        print(output)

    def order_info_list(sn, url10, token, version):  # post请求
        # url = 'https://f.triumen.cn/device/loglist'
        headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        data = {
            'limit': '10',
            'sn': sn,
            'token': token,
            'version': version
        }
        response = requests.request("post", url=url10, headers=headers, data=data)
        r = response.json()
        output = json.dumps(r, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        print(output)

    def orderlist(sn, url9, token, version):  # post请求
        # url = 'https://f.triumen.cn/device/tradelog'
        headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        data = {
            'max_time': '1577434658',  # 未想好用哪个时间，已写死
            'min_time': '1574842658',  # 未想好用哪个时间，已写死
            'sn': sn,
            'token': token,
            'version': version
        }
        response = requests.request("post", url=url9, headers=headers, data=data)
        r = response.json()
        output = json.dumps(r, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        print(output)

    def static_code(sn, url2, token, version, time):  # get请求
        # url = 'https://b.triumen.cn/pay/list?plat=dejk/z8='
        headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        params = {
            'time': time,
            'token': token,
            'sn': sn,
            'price': '1',
            'pay_type': '21',
            'version': version
        }
        response = requests.request("get", url=url2, headers=headers, params=params)
        # r = response.json()
        # output = json.dumps(r, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        print(response)

    def statistics(sn, url8, token, version):  # pos请求
        # url = 'https://f.triumen.cn/device/todaytrade'
        headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        data = {
            'sn': sn,
            'token': token,
            'version': version
        }
        response = requests.request("post", url=url8, headers=headers, data=data)
        r = response.json()
        output = json.dumps(r, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        print(output)

    def sweeping(sn, url5, token, version):  # get请求
        # url = 'https://b.triumen.cn/pay/list?plat=dejk/z8='
        headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        params = {
            'token': token,
            'sn': sn,
            'price': '1',
            'pay_type': '21',
            'version': version
        }
        response = requests.request("get", url=url5, headers=headers, params=params)
        # r = response.json()
        # output = json.dumps(r, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        print(response)

    def update(sn, url11, version, type):  # post请求
        # url = 'http://open.dairphone.cn/device/update'
        headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        data = {
            'sn': sn,
            'type': type,
            'version': version
        }
        response = requests.request("post", url=url11, headers=headers, data=data)
        r = response.json()
        output = json.dumps(r, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        print(output)

    def generate_md5(timestamp):
        print(timestamp)
        a = str(timestamp) + 'CMJF_getSignInfo'
        print(a)
        md5 = hashlib.md5()
        md5.update(a.encode('utf-8'))
        md5 = md5.hexdigest()
        return md5

    if __name__ == '__main__':
        sn = input('请输入SN：')
        version = input('请输入版本号（如1.0.0）：')
        timestamp = time.time()
        # md5_data = Md5Util.generate_md5(timestamp)
        md5_data = generate_md5(timestamp)
        config_list(sn, version, timestamp, md5_data)
        file = open('E:\\json.txt', 'r', encoding='utf-8')
        files = file.read()
        pattern0 = re.compile(r'https?://.*?"')
        pattern4 = re.compile(r'retail')
        result0 = pattern0.findall(files)
        result4 = pattern4.findall(files)
        file = open('E:\\json.txt', 'w', encoding='utf-8')
        for l in result0:
            url = l.replace('"', '')
            file.write(url + "\n")
        file.close()
        file = open('E:\\json.txt', 'r', encoding='utf-8')
        for num in range(12):
            num = num + 1
            url = linecache.getline('E:\\json.txt', lineno=num)
            print(url)
            if num == 1:
                if result4:
                    sign = 'sign'
                    activation(sn, url, version, timestamp, md5_data, sign)
                else:
                    sign = 'check_code'
                    activation(sn, url, version, timestamp, md5_data, sign)
                file = open('E:\\json.txt', 'r', encoding='utf-8')
                files = file.read()
                pattern1 = re.compile(r'"token":\s".*?"')
                pattern2 = re.compile(r'"uid":\s".*?"')
                pattern3 = re.compile(r'"type":\s".*?"')
                t = pattern1.findall(files)
                u = pattern2.findall(files)
                type = pattern3.findall(files)
                print(t)
                print(u)
                print(type)
                for t in t:
                    token1 = t.replace('"', '')
                    token = token1.replace('token: ', '')
                    print(token)
                for u in u:
                    uid1 = u.replace('"', '')
                    uid = uid1.replace('uid: ', '')
                    print(uid)
                for type in type:
                    type1 = type.replace('"', '')
                    type = type1.replace('type: ', '')
                    print(type)
            if num == 2:
                sweeping(sn, url, token, version)
            if num == 3:
                for num in range(30):
                    num = num + 1
                    back_sweep(sn, url, token, version)
            if num == 4:
                order(sn, url, token, version)
            if num == 5:
                static_code(sn, url, token, version, timestamp)
            if num == 6:
                card(sn, url, token, version, timestamp)
            if num == 7:
                check(sn, url, token, version)
            if num == 8:
                statistics(sn, url, token, version)
            if num == 9:
                orderlist(sn, url, token, version)
            if num == 10:
                order_info_list(sn, url, token, version)
            if num == 11:
                update(sn, url, version, type)
            if num == 12:
                log(sn, url, uid, version)
